package com.jader.activities;
/*确认订单，立即购买*/
import java.lang.reflect.Type;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jader.beans.Address;
import com.jader.beans.Goods;
import com.jader.beans.PayOrder;
import com.jader.beans.User;
import com.jader.myviews.MyProgressDialog;
import com.jader.myviews.PaymentPopWindow;
import com.jader.util.JaderApplication;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

public class BuyNow extends Activity implements OnClickListener {

	private RelativeLayout relative_buy_now;
	private TextView save_username, save_userphone, save_address,
			buy_now_sellername, buy_goods_name, buy_goods_desc,
			buy_goods_price, buy_goods_number, buy_reduce, buy_num, buy_add,
			buy_goods_count, conut_price_buynow_two, conut_price_buynow,
			confirm_order;
	private ImageView buy_goods_pic;
	private EditText buy_liuyan;
	private int buy_count = 1;
	private double goodsprice = 39.90;
	private Button confirm_buy_now;
	private Goods goods;
	private PaymentPopWindow popWindow;
	private View view;
	private int userid;
	private int resultcode = 1;
	private HttpUtils httpUtils;
	private Address address;
	private String url = JaderApplication.getIP() + "/Jader/client?type="
			+ JaderApplication.SELECT_ADDRESSBYUSERID + "&userid=";
	private MyProgressDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_buy_now);
		initViews();
		initData();
	}

	private void initViews() {
		dialog = JaderApplication.getMyDialog(this);
		relative_buy_now = (RelativeLayout) findViewById(R.id.relative_buy_now);
		save_username = (TextView) findViewById(R.id.save_username);
		save_userphone = (TextView) findViewById(R.id.save_userphone);
		save_address = (TextView) findViewById(R.id.save_address);
		buy_now_sellername = (TextView) findViewById(R.id.buy_now_sellername);
		buy_goods_pic = (ImageView) findViewById(R.id.buy_goods_pic);
		buy_goods_name = (TextView) findViewById(R.id.buy_goods_name);
		buy_goods_desc = (TextView) findViewById(R.id.buy_goods_desc);
		buy_goods_price = (TextView) findViewById(R.id.buy_goods_price);
		buy_goods_number = (TextView) findViewById(R.id.buy_goods_number);
		buy_num = (TextView) findViewById(R.id.buy_num);
		buy_reduce = (TextView) findViewById(R.id.buy_reduce);
		buy_add = (TextView) findViewById(R.id.buy_add);
		buy_liuyan = (EditText) findViewById(R.id.buy_liuyan);
		buy_goods_count = (TextView) findViewById(R.id.buy_goods_count);
		conut_price_buynow_two = (TextView) findViewById(R.id.conut_price_buynow_two);
		conut_price_buynow = (TextView) findViewById(R.id.conut_price_buynow);
		confirm_buy_now = (Button) findViewById(R.id.confirm_buy_now);
		confirm_order = (TextView) findViewById(R.id.confirm_order);
		view = findViewById(R.id.yingyin);
		httpUtils = new HttpUtils();
		popWindow = new PaymentPopWindow(this);
		relative_buy_now.setOnClickListener(this);
		buy_reduce.setOnClickListener(this);
		buy_add.setOnClickListener(this);
		confirm_buy_now.setOnClickListener(this);
		confirm_order.setOnClickListener(this);
		popWindow
				.setOnItemClickListener(new PaymentPopWindow.OnItemClickListener() {

					@Override
					public void onClickOKPop() {
						setBackgroundBlack(view, 1);

					}
				});
	}

	private void initData() {
		goods = (Goods) getIntent().getSerializableExtra("goods");
		userid = getIntent().getIntExtra("userid", 0);
		buy_count = getIntent().getIntExtra("number", 1);
		sendRequest(url + userid);
		buy_now_sellername.setText("羽芒玉器坊");
		JaderApplication.getBitmapUtils().display(buy_goods_pic,
				JaderApplication.getIP()+"/Jader"+ goods.getPicture());
		Log.e("商家图片", JaderApplication.getIP() + goods.getPicture());
		buy_goods_name.setText(goods.getGoodsName());
		buy_goods_desc.setText(goods.getDesc());
		buy_goods_price.setText("￥" + goods.getPrice());
		buy_liuyan.getText();
		initDynamicData();
	}

	private void initAddress(Address address) {
		save_username.setText("收货人：" + address.getReceiver());
		save_userphone.setText(address.getPhone());
		save_address.setText(address.getProvince() + address.getCity()
				+ address.getArea() + " " + address.getDetail());

	}

	private void initDynamicData() {
		goodsprice = goods.getPrice() * buy_count;
		String whatYourWant = JaderApplication.getPrice(goodsprice);
		buy_goods_count.setText("共" + buy_count + "件商品");
		conut_price_buynow_two.setText("￥" + whatYourWant);
		conut_price_buynow.setText("￥" + whatYourWant);
		buy_goods_number.setText("x" + buy_count);
		buy_num.setText(buy_count + "");
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.relative_buy_now:
			Intent intent = new Intent(this, AddressChoice.class);
			intent.putExtra("userid", userid);
			intent.putExtra("addressid", address.getAddressid());
			startActivityForResult(intent, resultcode);
			break;
		case R.id.buy_reduce:
			if (buy_count > 1) {
				buy_count--;
				initDynamicData();
			}

			break;
		case R.id.buy_add:
			if (buy_count < goods.getStock()) {
				buy_count++;
				initDynamicData();
			}

			break;

		case R.id.confirm_buy_now:
			String remark = buy_liuyan.getText().toString();
			popWindow.payment(new PayOrder(goodsprice, 0, new User(userid,
					null, null, null, null, null, 0), goods, address,
					buy_count, remark, null, null, null));
			setBackgroundBlack(view, 0);
			popWindow.showAsDropDown(arg0);
			break;
		case R.id.confirm_order:
			finish();
			break;

		default:
			break;
		}

	}

	public void setBackgroundBlack(View view, int what) {
		switch (what) {
		case 0:
			view.setVisibility(View.VISIBLE);
			break;
		case 1:
			view.setVisibility(View.GONE);
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == Activity.RESULT_OK && requestCode == this.resultcode) {
			address = (Address) data.getSerializableExtra("address");
			initAddress(address);
		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	private void sendRequest(String url) {
		dialog.show();
		httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {

			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				Gson gson = new Gson();
				Type type = new TypeToken<List<Address>>() {
				}.getType();
				List<Address> addresses = gson.fromJson(arg0.result, type);
				if (addresses.size() > 0) {
					address = addresses.get(0);
					initAddress(address);
				}
				if (dialog.isShowing()) {
					dialog.dismiss();
				}
			}
		});
	}

}
